﻿#region

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GalaSoft.MvvmLight.Command;
using NetBay.Core.Silverlight.Log;
using Rio.Framework.Localisation;

#endregion

namespace Rio.Framework.Pages
{
    public partial class MessageBoxChildWindow
    {
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof (string), typeof (MessageBoxChildWindow), null);

        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof (string), typeof (MessageBoxChildWindow), null);

        public static readonly DependencyProperty MessageDetailProperty =
            DependencyProperty.Register("MessageDetail", typeof (string), typeof (MessageBoxChildWindow), null);

        /// <summary>
        /// Initializes a new instance of the <see cref="MessageBoxChildWindow"/> class.
        /// </summary>
        public MessageBoxChildWindow()
        {
            InitializeComponent();

            InitLabel();

            KeyUp += MessageBoxChildWindow_KeyUp;

            ButtonClickCommand = new RelayCommand<string>(ButtonClick);

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MessageBoxChildWindow"/> class.
        /// </summary>
        /// <param name="p_szTitle">The P_SZ title.</param>
        /// <param name="p_szMessage">The P_SZ message.</param>
        /// <param name="p_oButtons">The p_o buttons.</param>
        /// <param name="p_oIcon">The p_o icon.</param>
        /// <param name="p_bTrace">if set to <c>true</c> [P_B trace].</param>
        /// <param name="p_eTraceLevel">The p_e trace level.</param>
        /// <param name="p_sMessageDetail">The P_S message detail.</param>
        public MessageBoxChildWindow(string p_szTitle, string p_szMessage, MessageBoxButtons p_oButtons,
                                     MessageBoxIcon p_oIcon, bool p_bTrace = true,
                                     Logger.Level p_eTraceLevel = Logger.Level.Debug, string p_sMessageDetail = null)
            : this()
        {
            Title = p_szTitle;
            Message = p_szMessage;
            MessageDetail = p_sMessageDetail;
            Buttons = p_oButtons;
            Icon = p_oIcon;

            ManageButtons();
            ManageIcon();
        }

        public string Title
        {
            get { return (string) GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public string Message
        {
            get { return (string) GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        public string MessageDetail
        {
            get { return (string) GetValue(MessageDetailProperty); }
            set { SetValue(MessageDetailProperty, value); }
        }

        public new MessageBoxIcon Icon { get; set; }
        public MessageBoxButtons Buttons { get; set; }
        public MessageBoxResult Result { get; set; }
        public RelayCommand<string> ButtonClickCommand { get; set; }

        /// <summary>
        /// Handles the KeyUp event of the MessageBoxChildWindow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
        private void MessageBoxChildWindow_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                Result = MessageBoxResult.Cancel;
                DialogResult = false;
                Close();
            }
            //else if (e.Key == Key.Enter)
            //{
            //    Result = MessageBoxResult.OK;
            //    DialogResult = true;
            //    Close();
            //}
        }

        /// <summary>
        /// Inits the label.
        /// </summary>
        private void InitLabel()
        {
            okButton.Content = Resx.GetLabel(ResxCommon.OK);
            cancelButton.Content = Resx.GetLabel(ResxCommon.CANCEL);
            yesButton.Content = Resx.GetLabel(ResxCommon.YES);
            noButton.Content = Resx.GetLabel(ResxCommon.NO);
        }

        /// <summary>
        /// Manages the buttons.
        /// </summary>
        private void ManageButtons()
        {
            switch (Buttons)
            {
                case MessageBoxButtons.Ok:
                    okButton.Visibility = Visibility.Visible;
                    cancelButton.Visibility = Visibility.Collapsed;
                    noButton.Visibility = Visibility.Collapsed;
                    yesButton.Visibility = Visibility.Collapsed;
                    break;

                case MessageBoxButtons.OkCancel:
                    okButton.Visibility = Visibility.Visible;
                    cancelButton.Visibility = Visibility.Visible;
                    noButton.Visibility = Visibility.Collapsed;
                    yesButton.Visibility = Visibility.Collapsed;
                    break;

                case MessageBoxButtons.YesNo:
                    okButton.Visibility = Visibility.Collapsed;
                    cancelButton.Visibility = Visibility.Collapsed;
                    noButton.Visibility = Visibility.Visible;
                    yesButton.Visibility = Visibility.Visible;
                    break;

                case MessageBoxButtons.YesNoCancel:
                    okButton.Visibility = Visibility.Collapsed;
                    cancelButton.Visibility = Visibility.Visible;
                    noButton.Visibility = Visibility.Visible;
                    yesButton.Visibility = Visibility.Visible;
                    break;
            }
        }

        /// <summary>
        /// Manages the icon.
        /// </summary>
        private void ManageIcon()
        {
            switch (Icon)
            {
                case MessageBoxIcon.Error:
                    msgIcon.Source = new BitmapImage(new Uri("../Assets/Images/MessageBox/error.png", UriKind.Relative));
                    ModalBackground = Resources["ErrorModalBackground"] as LinearGradientBrush;
                    break;

                case MessageBoxIcon.Question:
                    msgIcon.Source =
                        new BitmapImage(new Uri("../Assets/Images/MessageBox/question.png", UriKind.Relative));
                    ModalBackground = Resources["DefaultModalBackground"] as LinearGradientBrush;
                    break;

                case MessageBoxIcon.Warning:
                    msgIcon.Source =
                        new BitmapImage(new Uri("../Assets/Images/MessageBox/warning.png", UriKind.Relative));
                    ModalBackground = Resources["WarningModalBackground"] as LinearGradientBrush;
                    break;

                case MessageBoxIcon.None:
                case MessageBoxIcon.Information:
                    msgIcon.Source = new BitmapImage(new Uri("../Assets/Images/MessageBox/info.png", UriKind.Relative));
                    ModalBackground = Resources["InfoModalBackground"] as LinearGradientBrush;
                    break;
            }
        }

        /// <summary>
        /// Buttons the click.
        /// </summary>
        /// <param name="parameter">The parameter.</param>
        private void ButtonClick(string parameter)
        {
            switch (parameter.ToLowerInvariant())
            {
                case "ok":
                    DialogResult = true;
                    Result = MessageBoxResult.OK;
                    break;
                case "cancel":
                    DialogResult = false;
                    Result = MessageBoxResult.Cancel;
                    break;
                case "yes":
                    DialogResult = true;
                    Result = MessageBoxResult.Yes;
                    break;
                case "no":
                    DialogResult = false;
                    Result = MessageBoxResult.No;
                    break;
            }

            Close();
        }

        /// <summary>
        /// Shows this instance.
        /// </summary>
        public new void Show()
        {
            base.ShowDialog();
        }
    }

    public enum MessageBoxButtons
    {
        Ok,
        YesNo,
        YesNoCancel,
        OkCancel
    }

    public enum MessageBoxIcon
    {
        Question,
        Information,
        Error,
        None,
        Warning
    }
}